home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 264_01 / nro.c < prev    next >
Text File  |  1980-01-01  |  4KB  |  279 lines

  1. /*
  2.  *    Word Processor
  3.  *    similar to Unix NROFF or RSX-11M RNO -
  4.  *    adaptation of text processor given in
  5.  *    "Software Tools", Kernighan and Plauger.
  6.  *
  7.  *    Stephen L. Browning
  8.  *    5723 North Parker Avenue
  9.  *    Indianapolis, Indiana 46220
  10.  *
  11.  *    adapated to ms-dos 10/17/87 pjv
  12.  *
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include "nro.h"
  18. #include "nrodef.c"
  19.  
  20. main(argc,argv)
  21. int argc;
  22. char *argv[];
  23. {
  24.     FILE *ifp, *ofp;
  25.     int i;
  26.     int swflg;
  27.  
  28.     swflg = FALSE;
  29.     pout = stdout;
  30.     ifp = stdin;
  31.     ofp = stdout;
  32.     init();
  33.     for (i=1; i<argc; ++i)
  34.     {
  35.         if (*argv[i] == '-' || *argv[i] == '+')
  36.         {
  37.             if (pswitch(argv[i],&swflg) == ERR)
  38.                 exit(-1);
  39.         }
  40.     }
  41.     for (i=1; i<argc; ++i)
  42.     {
  43.         if (*argv[i] != '-' && *argv[i] != '+')
  44.         {
  45.             if ((ifp = sofile[0] = fopen(argv[i],"r")) == NULL)
  46.             {
  47.                 fprintf(stderr, "nro: unable to open file %s\n",argv[i]);
  48.                 exit(-1);
  49.             }
  50.             else
  51.             {
  52.                 profile();
  53.                 fclose(sofile[0]);
  54.             }
  55.         }
  56.     }
  57.     if ((ifp == stdin && swflg == FALSE) || argc <= 1)
  58.     {
  59.         puts("Usage: nro [-n] [+n] [-pxx] [-v] [-b] [-mmacfile] infile ... [>outfile]\n");
  60.         exit(-1);
  61.     }
  62.     if (pout != stdout)
  63.     {
  64.         fflush(pout);
  65.         fclose(pout);
  66.     }
  67. }
  68.  
  69.  
  70.  
  71. /*
  72.  *    retrieve one line of input text
  73.  */
  74.  
  75. getlin(p, in_buf)
  76. char *p;
  77. FILE *in_buf;
  78. {
  79.     int i;
  80.     int c;
  81.     char *q;
  82.  
  83.     q = p;
  84.     for (i=0; i<MAXLINE-1; ++i)
  85.     {
  86.         c = ngetc(in_buf);
  87.         if(c == EOF)
  88.         {
  89.             *q = '\0';
  90.             c = strlen(p);
  91.             return(c == 0 ? EOF : c);
  92.         }
  93.         *q++ = c;
  94.         if (c == '\n') break;
  95.     }
  96.     *q = '\0';
  97.     return(strlen(p));
  98. }
  99.  
  100.  
  101.  
  102. /*
  103.  *    initialize parameters for nro word processor
  104.  */
  105.  
  106. init()
  107. {
  108.     int i;
  109.  
  110.     dc.fill = YES;
  111.     dc.lsval = 1;
  112.     dc.inval = 0;
  113.     dc.rmval = PAGEWIDTH - 1;
  114.     dc.tival = 0;
  115.     dc.ceval = 0;
  116.     dc.ulval = 0;
  117.     dc.cuval = 0;
  118.     dc.juval = YES;
  119.     dc.boval = 0;
  120.     dc.bsflg = FALSE;
  121.     dc.pgchr = '#';
  122.     dc.cmdchr = '.';
  123.     dc.prflg = TRUE;
  124.     dc.sprdir = 0;
  125.     for (i=0; i<26; ++i) dc.nr[i] = 0;
  126.     pg.curpag = 0;
  127.     pg.newpag = 1;
  128.     pg.lineno = 0;
  129.     pg.plval = PAGELEN;
  130.     pg.m1val = 2;
  131.     pg.m2val = 2;
  132.     pg.m3val = 2;
  133.     pg.m4val = 2;
  134.     pg.bottom = pg.plval - pg.m4val - pg.m3val;
  135.     pg.offset = 0;
  136.     pg.frstpg = 0;
  137.     pg.lastpg = 30000;
  138.     pg.ehead[0] = pg.ohead[0] = '\n';
  139.     pg.efoot[0] = pg.ofoot[0] = '\n';
  140.     for (i=1; i<MAXLINE; ++i) {
  141.         pg.ehead[i] = pg.ohead[i] = '\0';
  142.         pg.efoot[i] = pg.ofoot[i] = '\0';
  143.     }
  144.     pg.ehlim[LEFT] = pg.ohlim[LEFT] = dc.inval;
  145.     pg.eflim[LEFT] = pg.oflim[LEFT] = dc.inval;
  146.     pg.ehlim[RIGHT] = pg.ohlim[RIGHT] = dc.rmval;
  147.     pg.eflim[RIGHT] = pg.oflim[RIGHT] = dc.rmval;
  148.     co.outp = 0;
  149.     co.outw = 0;
  150.     co.outwds = 0;
  151.     co.lpr = FALSE;
  152.     for (i=0; i<MAXLINE; ++i) co.outbuf[i] = '\0';
  153.     for (i=0; i<MXMDEF; ++i) mac.mnames[i] = NULL;
  154.     mac.lastp = 0;
  155.     mac.emb = &mac.mb[0];
  156.     mac.ppb = NULL;
  157. }
  158.  
  159.  
  160. /*
  161.  *    get character from input file or push back buffer
  162.  */
  163.  
  164. ngetc(infp)
  165. FILE *infp;
  166. {
  167.     int c;
  168.  
  169.     if (mac.ppb >= &mac.pbb[0]) {
  170.         c = *mac.ppb--;
  171.     }
  172.     else
  173.     {
  174.         c = getc(infp);
  175.     }
  176.     return(c);
  177. }
  178.  
  179.  
  180.  
  181. /*
  182.  *    process input files from command line
  183.  */
  184.  
  185. profile()
  186. {
  187.     char ibuf[MAXLINE];
  188.  
  189.     for (dc.flevel=0; dc.flevel>=0; --dc.flevel)
  190.     {
  191.         while (getlin(ibuf,sofile[dc.flevel]) != EOF)
  192.         {
  193.             if (ibuf[0] == dc.cmdchr)
  194.                 comand(ibuf);
  195.             else 
  196.                 text(ibuf);
  197.         }
  198.         if(dc.flevel > 0)
  199.             fclose(sofile[dc.flevel]);
  200.     }
  201.     if(pg.lineno > 0)
  202.         space(HUGE);
  203. }
  204.  
  205.  
  206.  
  207. /*
  208.  *    process switch values from command line
  209.  */
  210.  
  211. pswitch(p,q)
  212. char *p;
  213. int *q;
  214. {
  215.     int swgood;
  216.  
  217.     swgood = TRUE;
  218.     if (*p == '-')
  219.     {
  220.         ++p;
  221.         switch (tolower(*p))
  222.         {
  223.         case 'b':
  224.             dc.bsflg = TRUE;
  225.             break;
  226.  
  227.         case 'm':
  228.             if ((sofile[0] = fopen(++p,"r")) == (FILE *)0)
  229.             {
  230.                 fprintf(stderr, "nro: unable to open file %s\n",p);
  231.                 exit(-1);
  232.             }
  233.             profile();
  234.             fclose(sofile[0]);
  235.             break;
  236.  
  237.         case 'p':
  238.             set(&pg.offset,ctod(++p),'1',0,0,HUGE);
  239.             break;
  240.  
  241.         case 'v':
  242.             printf("nro ms-dos version 1.0\n");
  243.             *q = TRUE;
  244.             break;
  245.  
  246.         case '0':
  247.         case '1':
  248.         case '2':
  249.         case '3':
  250.         case '4':
  251.         case '5':
  252.         case '6':
  253.         case '7':
  254.         case '8':
  255.         case '9':
  256.             pg.lastpg = ctod(p);
  257.             break;
  258.  
  259.         default:
  260.             swgood = FALSE;
  261.             break;
  262.         }
  263.     }
  264.     else if (*p == '+')
  265.     {
  266.         pg.frstpg = ctod(++p);
  267.     }
  268.     else
  269.     {
  270.         swgood = FALSE;
  271.     }
  272.     if (swgood == FALSE)
  273.     {
  274.         printf("nro: illegal switch %s\n",p);
  275.         return(ERR);
  276.     }
  277.     return(OK);
  278. }
  279.